<template>
  <div class="parent">
    <echartsDom v-if="isShowEcharts" :width="width" :height="height" :option="option" />
    <div v-else>
      <px-empty :image-size="50" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, ref, watch } from "vue";
import echartsDom from "./echartsDom.vue";
import { queryConsumeChartForDay } from "@/api/waterKanban";
const props = defineProps({
  width: {
    type: String,
    default: ""
  },
  height: {
    type: String,
    default: ""
  },
  color: {
    type: String,
    default: "#3B82F6"
  },
  chartvalue: {
    type: Object,
    required: true
  }
});
const isShowEcharts = ref(false);
const state = reactive({
  option: {},
  series: []
});
const { option, series } = toRefs(state);

const getoptions = () => {
  console.log(props.chartvalue);
  if (props.chartvalue != null && props.chartvalue.xaxis) {
    isShowEcharts.value = true;
    series.value = [
      {
        name: "本周期",
        type: "line",
        data: props.chartvalue.curYAxis
      },
      {
        name: "上一周期",
        type: "line",
        data: props.chartvalue.pastYAxis
      }
    ];

    //   props.chartvalue.yaxis.map((item, index) => {
    //   return {
    //     name: item.name,
    //     type: "line",
    //     data: item.value
    //   };
    // });
    option.value = {
      color: ["#3B82F6 ", "#39C670"],
      toolbox: {
        show: true,
        feature: {
          saveAsImage: {
            title: "点击保存为图片"
          }
        }
      },
      grid: {
        left: "20px",
        top: "40px",
        right: "10px",
        bottom: "10px",
        containLabel: true
      },
      legend: {
        x: "center"
      },
      tooltip: {
        trigger: "axis"
      },
      xAxis: {
        type: "category",
        boundarGap: false,
        data: props.chartvalue.xaxis
        // data: ["2024-1", "2024-2", "2024-3", "2024-4", "2024-5", "2024-6", "2024-7", "2024-8", "2024-9", "2024-10", "2024-11", "2024-12"]
      },
      yAxis: {
        name: "tCO₂e",
        type: "value"
      },
      series: series
    };
  } else {
    isShowEcharts.value = false;
  }
};
watch(
  () => props.chartvalue,
  newOption => {
    console.log(newOption);
    getoptions();
  },
  { immediate: true }
);
</script>

<style scoped lang="scss">
.parent {
  float: left;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
